﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BubbleBlend : MonoBehaviour
{
    [Header("融合参数")]
    public float Movespeed = 1;
    public float blenddst = 1;
    public float BlendCoef = 3;
    public float BlendCoef2 = 0.1f;
    public string blendState;
    GameObject target;

    [Header("泡泡中心坐标")]
    public Vector3 CenterPos;

    [Header("多级大泡泡")]
    GameObject Secbubble;
    public GameObject _3bubble;
    public GameObject _4bubble;
    public GameObject _5bubble;
    public GameObject _6bubble;
    public Color bigColor;
    Color ColorTemp;
    Color ColorInitial;
    Color Sec0Color;
    Material Mtemp;
    Transform secTrans;

    // Start is called before the first frame update
    void Start()
    {
        blendState = "Not";
        target = GameObject.FindGameObjectWithTag("SHPERE2");
        Secbubble = GameObject.FindGameObjectWithTag("2Bubble");
        ColorInitial = GetComponent<MeshRenderer>().material.color;
        Material SecMtemp = Secbubble.GetComponent<MeshRenderer>().material;
        Mtemp = GetComponent<MeshRenderer>().material;
        //Secbubble.GetComponent<MeshRenderer>().material.color = bigColor;
        Secbubble.GetComponent<MeshRenderer>().material.color = new Color(SecMtemp.color.r, SecMtemp.color.g, SecMtemp.color.b, 0.0f);
        Sec0Color = Secbubble.GetComponent<MeshRenderer>().material.color;
        ColorTemp = new Color(SecMtemp.color.r, SecMtemp.color.g, SecMtemp.color.b, 0.0f);
        secTrans = Secbubble.transform;
        //Secbubble.SetActive(false);
    }
    // Update is called once per frame
    void Update()
    {
        BlendTest();
        BlendState();
        //Debug.Log(Secbubble.GetComponent<MeshRenderer>().material.color);
    }
    void BlendTest()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("space");
            Debug.Log((target.transform.position - transform.position).magnitude);
            //if(CollisionWitYello)
            if ((target.transform.position - transform.position).magnitude < blenddst)
            {
                Debug.Log("start");
                blendState = "START";
            }
        }
        if (Input.GetKeyDown(KeyCode.L))
        {
            if ((target.transform.position - transform.position).magnitude < blenddst)
            {
                blendState = "STARTsplit";
            }
        }
    }
    //融合开始
    void BlendState()
    {
        if (blendState == "START")
        {
            ColorTemp.a = 0;
            CenterPos = (target.transform.position + transform.position) / 2.0f;
            blendState = "ING";
            //transform.LookAt(target.transform.position);
            //transform.Translate(Vector3.forward * blendspeed * Time.deltaTime);
            //Invoke("BubbleFade",1.0f);
        }
        else if (blendState == "ING")
        {
            Secbubble.transform.position = CenterPos;
            transform.LookAt(CenterPos);
            //Invoke("HugeBubbleIn", 0.001f);
            HugeBubbleIn();
            transform.Translate(Vector3.forward * Movespeed * Time.deltaTime);
            Invoke("FadeOut", 0.2f);
        }
        else if (blendState == "STARTsplit")
        {
            splitBubble();
        }
        //if ((transform.position - target.transform.position).magnitude < 0.01f)
        //{
        //    blendState = "NOT";
        //}
    }
    //泡泡的淡出
    void FadeOut()
    {
        GetComponent<MeshRenderer>().material.color = Color.Lerp(GetComponent<MeshRenderer>().material.color, Color.clear, BlendCoef * Movespeed * Time.deltaTime);
    }
    //泡泡的淡入
    void HugeBubbleIn()
    {
        //Color ColorTemp = new Color(GetComponent<MeshRenderer>().material.color.r, GetComponent<MeshRenderer>().material.color.g, GetComponent<MeshRenderer>().material.color.b, 1);
        if (Secbubble.GetComponent<MeshRenderer>().material.color.a < 0.4f)
        {
            StartCoroutine("FadeIn");
        }
        if (Secbubble.transform.localScale.x > 1.8f)
        {
            StartCoroutine("ScaleDown");
        }
    }

    IEnumerator FadeIn()
    {
        ColorTemp.a += 0.001f;
        Secbubble.GetComponent<MeshRenderer>().material.color = ColorTemp;
        yield return 0.5f;
    }

    IEnumerator ScaleDown()
    {
        secTrans.localScale = 0.99f * secTrans.localScale;
        Secbubble.transform.localScale = secTrans.localScale;
        yield return 1.5f;
    }

    void splitBubble()
    {
        Secbubble.GetComponent<MeshRenderer>().material.color = Sec0Color;
        GetComponent<MeshRenderer>().material.color = ColorInitial;
    }
}
